# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=cve-search
pkgver=v5.2.0.r123.g938fc57
pkgrel=1
epoch=1
pkgdesc='A tool to perform local searches for known vulnerabilities.'
groups=('blackarch' 'blackarch-exploitation')
arch=('any')
url='http://cve-search.github.io/cve-search'
license=('AGPL')
depends=('python' 'python-virtualenv')
makedepends=('git')
source=("git+https://github.com/$pkgname/$pkgname.git")
sha512sums=('SKIP')
install="$pkgname.install"

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

package() {
  cd "$pkgname"

  install -dm 755 "$pkgdir/usr/bin"
  install -dm 755 "$pkgdir/usr/share/$pkgname"

  install -Dm 644 -t "$pkgdir/usr/share/doc/$pkgname/" README.md
  install -Dm 644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  cp -a --no-preserve=ownership * "$pkgdir/usr/share/$pkgname/"

  cd bin/
  for script in *; do
    final_script_name="${pkgname}-${script%.*}"
    cat > "$pkgdir/usr/bin/$final_script_name" << EOF
#!/bin/sh
cd /usr/share/$pkgname/
source venv/bin/activate
exec python bin/$script "\$@"
EOF
  chmod +x "$pkgdir/usr/bin/$final_script_name"
  done

  cd ../sbin/
  for script in *; do
    final_script_name="${pkgname}-${script%.*}"
    cat > "$pkgdir/usr/bin/$final_script_name" << EOF
#!/bin/sh
cd /usr/share/$pkgname/
source venv/bin/activate
exec python sbin/$script "\$@"
EOF
  chmod +x "$pkgdir/usr/bin/$final_script_name"
  done

  rm "$pkgdir/usr/bin/"*__init*
}

